package com.gx.common.config.flowable;

import com.gx.listener.ProcessEndListener;
import com.gx.listener.ProcessStartListener;
import com.gx.listener.ProcessTimeListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Flowable添加全局监听器
 *
 * @author azhuzhu
 */
@Configuration
@RequiredArgsConstructor
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final ProcessTimeListener timeListener;
    private final ProcessEndListener processEndListener;
    private final ProcessStartListener processStartListener;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        // 流程超时监听器
        dispatcher.addEventListener(timeListener, FlowableEngineEventType.TIMER_FIRED);
        //流程结束监听器
        dispatcher.addEventListener(processEndListener, FlowableEngineEventType.PROCESS_COMPLETED);
        //流程启动监听器
        dispatcher.addEventListener(processStartListener, FlowableEngineEventType.PROCESS_STARTED);

    }

}
